package com.easylinkin.linkappapi.common.utils;

import com.easylinkin.linkappapi.common.constant.CommonConstant;

import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

public class StringUtil {

	private static Pattern underlinePattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
	private static Pattern camelPattern = Pattern.compile("[A-Z]([a-z\\d]+)?");

	private StringUtil() {

	}

	/**
	 * 下划线转驼峰法(默认小驼峰)
	 *
	 * @param lineName   源字符串
	 * @param smallCamel 大小驼峰,是否为小驼峰(驼峰，第一个字符是大写还是小写)
	 * @return 转换后的字符串
	 */
	public static String underline2Camel(String lineName, boolean... smallCamel) {
		if (StringUtils.isEmpty(lineName)) {
			return "";
		}
		StringBuilder sb = new StringBuilder();

		Matcher matcher = underlinePattern.matcher(lineName);
		// 匹配正则表达式
		while (matcher.find()) {
			String word = matcher.group();
			// 当是true 或则是空的情况
			boolean flag = (smallCamel.length == CommonConstant.ZERO || smallCamel[CommonConstant.ZERO])
					&& matcher.start() == CommonConstant.ZERO;
			if (flag) {
				sb.append(Character.toLowerCase(word.charAt(0)));
			} else {
				sb.append(Character.toUpperCase(word.charAt(0)));
			}

			int index = word.lastIndexOf('_');
			if (index > CommonConstant.ZERO) {
				sb.append(word.substring(CommonConstant.ONE, index).toLowerCase());
			} else {
				sb.append(word.substring(CommonConstant.ONE).toLowerCase());
			}
		}
		return sb.toString();
	}

	/**
	 * 驼峰法转下划线
	 *
	 * @param name 源字符串
	 * @return 转换后的字符串
	 */
	public static String camel2Underline(String name) {
		if (StringUtils.isEmpty(name)) {
			return "";
		}
		name = String.valueOf(name.charAt(0)).toUpperCase().concat(name.substring(1));
		StringBuilder sb = new StringBuilder();
		Matcher matcher = camelPattern.matcher(name);
		while (matcher.find()) {
			String word = matcher.group();
			sb.append(word.toUpperCase());
			sb.append(matcher.end() == name.length() ? "" : "_");
		}
		return sb.toString();
	}

	/**
	 * 首字母转小写
	 */
	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0))) {
			return s;
		} else {
			return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
		}
	}

	/**
	 * 首字母转大写
	 */
	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0))) {
			return s;
		} else {
			return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
		}
	}

	/**
	 * 将空字符串转为字符串零
	 */
	public static String nullToZero(String str) {
		if (StringHelper.isEmpty(str)) {
			return CommonConstant.ZERO_STR;
		}
		return str;
	}

	/**
	 * 把模糊查询的检索的关键字对特殊字符进行转换如“[”、“%”
	 *
	 * @param str 需要转换的字符串
	 * @return 返回模糊查询的字符串
	 */
	public static String toLikeStr(String str) {
		if (str != null && str.length() > 0) {
			String replace = str.trim().replace("\\", "\\\\%");
			String replace1 = replace.replace("%", "\\%");
			str = replace1.replace("_", "\\_");
		}
		return str;
	}

	/**
	 * 将数字转换布尔值，大于1时返回true
	 *
	 * @return boolean
	 */
	public static boolean retBool(Integer result) {
		return null != result && result >= 1;
	}

	/**
	 * 将字符串转成double类型值 如果入参为非数字字符串或者为空将返回0.0
	 */
	public static double string2Double(String s) {
		double result = 0.0;
		if (org.apache.commons.lang3.StringUtils.isBlank(s)) {
			return result;
		}

		try {
			return Double.parseDouble(s);
		} catch (NumberFormatException e) {
			return result;
		}
	}

	public static String getStatus(String status) {
		if (StringUtils.isEmpty(status)) {
			return CommonConstant.ZERO_STR;
		}
		return status;
	}

	/**
	 * 生成唯一字符串UUID
	 * 
	 * @return
	 */
	public static String getUUID() {
		String id = UUID.randomUUID().toString();
		id = id.replaceAll("-", "");
		return id;
	}

	/**
	 * 根据指定长度获取对应长度的数字字符串
	 * 
	 * @param len
	 * @return
	 */
	public static String getRandomByLength(int len) {
		int rs = (int) ((Math.random() * 9 + 1) * Math.pow(10, len - 1));
		return String.valueOf(rs);
	}

	/**
	 * 覆盖特殊字符，否则sheetname 校验不通过导出会失败
	 *
	 * @param s input
	 * @return 过滤后的字符串 参见
	 * @see org.apache.poi.ss.util.WorkbookUtil#validateSheetName(java.lang.String)
	 *      <p>
	 *      冒号——： 斜杠——/ 问号——？ 反斜杠——\ 星号——* 方符号——[ 或 ] 构成工作表的表名字符个数不得超过31个
	 */
	public static String coverSheetName(String s) {
		if (s == null || s.length() == 0) {
			return "-";
		}
//        :,/ ,\,*,[,],?,|
		String res = s.replaceAll(":|/|\\*|\\[|\\]|\\?|\\|", "");
		res = res.replaceAll("\\\\", "");
		if (res.length() > 31) {
			res = res.substring(0, 31);
		}
		return res;
	}

	public static void main(String[] args) {
		String s = ":3/44?54::??*67/\\\\*|[]-2||\\90\\\\90\\\\\\09\\\\\\\\10\\\\\\\\\\90\\999999999999999";
		System.out.println(coverSheetName(s));
		System.out.println(coverSheetName(s).length());
	}

	/**
	 * 获取长度为 16 的随机字母+数字
	 * 
	 * @return 随机数字
	 */
	public static String getRandomNumber() {
		
		String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 数字和26个字母组成

		Random RANDOM = new SecureRandom();

		char[] nonceChars = new char[16]; // 指定长度为6位/自己可以要求设置

		for (int index = 0; index < nonceChars.length; ++index) {
			nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
		}
		return new String(nonceChars);
	}
}
